﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using PmuRegistry.Silverlight.Utilities;
using PmuRegistry.Silverlight.ModalDialogs;

namespace PmuRegistry.Silverlight.Pages.Manage
{
    public partial class SystemSettings : Page
    {
        public SystemSettings()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(SystemSettings_Loaded);
            ButtonSave.Click += new RoutedEventHandler(ButtonSave_Click);
            ButtonClear.Click += new RoutedEventHandler(ButtonClear_Click);
        }

        void ButtonClear_Click(object sender, RoutedEventArgs e)
        {
            Storyboard sb = new Storyboard();
            sb = Application.Current.Resources["ButtonPressAnimation"] as Storyboard;
            sb.Completed += new EventHandler(delegate(object obj, EventArgs es) { sb.Stop(); });
            Storyboard.SetTarget(sb, ButtonClearTransform);
            sb.Begin();

            //Load Default Settings.
            Common.SetDefaultSystemSettings(true);
            LoadSettingsFromIsolatedStorage();

            SystemMessages sm = new SystemMessages(new Message() { UserMessage = "Successfully Restored Default System Settings", SystemMessage = string.Empty, UserMessageType = MessageType.Success },
                        ButtonType.OkOnly);
            sm.Show();
        }

        void ButtonSave_Click(object sender, RoutedEventArgs e)
        {
            Storyboard sb = new Storyboard();
            sb = Application.Current.Resources["ButtonPressAnimation"] as Storyboard;
            sb.Completed += new EventHandler(delegate(object obj, EventArgs es) { sb.Stop(); });
            Storyboard.SetTarget(sb, ButtonSaveTransform);
            sb.Begin();

            if (!string.IsNullOrEmpty(TextBoxDefaultWidth.Text))
                IsolatedStorageManager.SaveIntoIsolatedStorage("DefaultWidth", Convert.ToInt32(TextBoxDefaultWidth.Text));

            if (!string.IsNullOrEmpty(TextBoxDefaultHeight.Text))
                IsolatedStorageManager.SaveIntoIsolatedStorage("DefaultHeight", Convert.ToInt32(TextBoxDefaultHeight.Text));

            if (!string.IsNullOrEmpty(TextBoxMinimumWidth.Text))
                IsolatedStorageManager.SaveIntoIsolatedStorage("MinimumWidth", Convert.ToInt32(TextBoxMinimumWidth.Text));

            if (!string.IsNullOrEmpty(TextBoxMinimumHeight.Text))
                IsolatedStorageManager.SaveIntoIsolatedStorage("MinimumHeight", Convert.ToInt32(TextBoxMinimumHeight.Text));

            IsolatedStorageManager.SaveIntoIsolatedStorage("ResizeWithBrowser", CheckboxResizeWithBrowser.IsChecked);

            IsolatedStorageManager.SaveIntoIsolatedStorage("MaintainAspectRatio", CheckboxMaintainAspectRatio.IsChecked);

            if (!string.IsNullOrEmpty(TextBoxNumberOfMessagesOnMonitor.Text))
                IsolatedStorageManager.SaveIntoIsolatedStorage("NumberOfMessagesOnMonitor", Convert.ToInt32(TextBoxNumberOfMessagesOnMonitor.Text));

            LoadSettingsFromIsolatedStorage();

            SystemMessages sm = new SystemMessages(new Message() { UserMessage = "Successfully Saved System Settings", SystemMessage = string.Empty, UserMessageType = MessageType.Success },
                        ButtonType.OkOnly);
            sm.Show();
        }

        void SystemSettings_Loaded(object sender, RoutedEventArgs e)
        {
            LoadSettingsFromIsolatedStorage();
        }

        void LoadSettingsFromIsolatedStorage()
        {
            TextBoxDefaultWidth.Text = IsolatedStorageManager.LoadFromIsolatedStorage("DefaultWidth").ToString();
            TextBoxDefaultHeight.Text = IsolatedStorageManager.LoadFromIsolatedStorage("DefaultHeight").ToString();
            TextBoxMinimumWidth.Text = IsolatedStorageManager.LoadFromIsolatedStorage("MinimumWidth").ToString();
            TextBoxMinimumHeight.Text = IsolatedStorageManager.LoadFromIsolatedStorage("MinimumHeight").ToString();
            TextBoxNumberOfMessagesOnMonitor.Text = IsolatedStorageManager.LoadFromIsolatedStorage("NumberOfMessagesOnMonitor").ToString();
            CheckboxResizeWithBrowser.IsChecked = (bool)IsolatedStorageManager.LoadFromIsolatedStorage("ResizeWithBrowser");
            CheckboxMaintainAspectRatio.IsChecked = (bool)IsolatedStorageManager.LoadFromIsolatedStorage("MaintainAspectRatio");
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

    }
}
